package com.dqp.cslggroup.Util;

import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.MyApplication;


public class ToastUtil {

    public static void showToast(String msg) {
        if (isMainThread()) {
            myToast(msg,Toast.LENGTH_SHORT);
            //Toast.makeText(MyApplication.getContext(), msg, Toast.LENGTH_SHORT).show();
        } else {
            Looper.prepare();
            myToast(msg,Toast.LENGTH_SHORT);
            //Toast.makeText(MyApplication.getContext(), msg, Toast.LENGTH_SHORT).show();
            Looper.loop();
        }
    }

    public static void showLongToast(String msg) {
        if (isMainThread()) {
            myToast(msg,Toast.LENGTH_LONG);
            //Toast.makeText(MyApplication.getContext(), msg, Toast.LENGTH_LONG).show();
        } else {
            Looper.prepare();
            myToast(msg,Toast.LENGTH_LONG);
            //Toast.makeText(MyApplication.getContext(), msg, Toast.LENGTH_LONG).show();
            Looper.loop();
        }
    }

    private static boolean isMainThread() {
        return Looper.getMainLooper().getThread() == Thread.currentThread();
    }

    public static void  myToast(String msg,int duration){
        Toast toast=new Toast(MyApplication.getContext());
        LayoutInflater inflater = LayoutInflater.from(MyApplication.getContext());
        View view =inflater.inflate(R.layout.mytoast,null);
        TextView textView=view.findViewById(R.id.msg);
        textView.setText(msg);
        toast.setView(view);
        toast.setDuration(duration);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

}
